define(function(require, exports, module){
    require('basic');
    require('highstock');
    var config = require('config');
    var user = require('user');
    var model = function(){
        this.init();
    };
    model.prototype = {
        /**
         * 编辑首页侧边导航快捷项，有些显示有些不显示
         * @return {[type]} [description]
         */
        editQuickNav: function(){
            var sf = this;
            var cookie_arr = $.cookie('quick_nav') ? $.cookie('quick_nav').split(',') : []; //取cookie
            var wrap = $('#quick_nav'),
                submit_bar = wrap.find('.submit_bar');
            /**
             * [creatCover description]
             * @param  {[type]} item [description]
             * @return {[type]}      [description]
             */
            function creatCover(item){
                var submit_bar = wrap.find('.submit_bar'),
                    ind = Number($(item).index()),
                    td = $(item).find('td').eq(1),
                    cover = $('<div class="cover"><span>hide</span> | <span>show</span></div>');
                cover.appendTo(td);
                cover.find('span').each(function(index,el){
                    $(el).click(function(){
                        cookie_arr = $.grep(cookie_arr, function(n,i){
                          return Number(n) != ind;
                        });
                        if(index==0){
                            cookie_arr.push(ind.toString());
                            $(item).addClass('disable').removeClass('live');
                        }else{
                            $(item).removeClass('disable').addClass('live');
                        };
                    });
                });
            };
            /**
             * [removeCover description]
             * @return {[type]} [description]
             */
            function removeCover(){
                wrap.find('.cover').remove();
            }
            $('#quick_nav_dropdown li a').each(function(index,el){ 
                if(index==0){
                    $(el).on('click',function(e){ //下拉edit进入编辑状态
                        submit_bar.show();
                        wrap.addClass('edit');
                        wrap.find('tr').each(function(ind,itm){
                            var bl = false;
                            for(var i=0;i<cookie_arr.length;i++){
                                var num = Number(cookie_arr[i]);
                                if(ind==num){
                                    bl = true;
                                };
                            };
                            if(bl){
                                $(itm).addClass('disable');
                            }else{
                                $(itm).addClass('live');
                            };
                            creatCover(itm);
                        });
                    });
                    submit_bar.find('.form_submit').on('click',function(){ //提交好恢复菜单正常状态
                        wrap.removeClass('edit');
                        submit_bar.hide();
                        removeCover()
                        $.cookie('quick_nav', cookie_arr, { expires: 365 }); // 提交确定存入cookie
                        sf.editQuickNav();
                    });
                };
            });
        },
        /**
         * [loadQuickNav description]
         * @return {[type]} [description]
         */
        loadQuickNav: function(){
            var cookie_arr = $.cookie('quick_nav') ? $.cookie('quick_nav').split(',') : [],
                wrap = $('#quick_nav');
            wrap.find('tr').each(function(ind,itm){
                var bl = false;
                for(var i=0;i<cookie_arr.length;i++){
                    var num = Number(cookie_arr[i]);
                    if(ind==num){
                        bl = true;
                    };
                };
                if(bl){
                    $(itm).addClass('disable');
                }else{
                    $(itm).addClass('live');
                };
            });
            this.editQuickNav();//编辑快捷菜单
        },
        /**
         * 左边导航悬浮
         * @return {[type]} [description]
         */
        fixedLeftNavWrap: function(){
            var $win = $(window), 
                $nav = $('#quick_nav'), 
                navTop = $('#quick_nav').length && $('#quick_nav').offset().top, 
                isFixed = 0;
            function processScroll() {
                var i, scrollTop = $win.scrollTop();
                    if (scrollTop >= navTop && !isFixed) {
                        isFixed = 1;
                        $nav.addClass('subnav-fixed');
                    } else if (scrollTop <= navTop && isFixed) {
                        isFixed = 0;
                        $nav.removeClass('subnav-fixed');
                    }
            }
            $win.on('scroll', processScroll);
        },
        init: function(){
            var sf = this;
            this.loadQuickNav(); //载入快捷菜单
            this.fixedLeftNavWrap();
        }
    };

    module.exports = new model();
});
